<template>
  <div class="app-container">
    <div class="mb10">
      <el-button type="success" icon="el-icon-plus" @click="handleAdd"> 添加 </el-button>
    </div>

    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row size="mini">
      <el-table-column label="#" type="index" width="50" />
      <el-table-column label="类型" prop="type">
        <template slot-scope="{ row }">{{ contactKV[row.type] }} </template>
      </el-table-column>
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="电话" prop="telephone" />
      <el-table-column label="手机" prop="mobile" />
      <el-table-column label="电邮" prop="email" />
      <el-table-column label="备注" prop="comment" />
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(row)" />
          <!-- <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          /> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="m-center">
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchList" />
    </div>

    <!-- 编辑 -->
    <el-dialog :title="isEdit ? '编辑' : '新增'" :visible.sync="dialogVisible" width="500px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px">
        <el-form-item label="类型" prop="type" required>
          <el-select v-model="temp.type" placeholder="请选择类型">
            <el-option v-for="item in contactOptions" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model.trim="temp.name" autocomplete="off" placeholder="请输入姓名" />
        </el-form-item>

        <el-form-item label="电话" prop="telephone">
          <el-input v-model.trim="temp.telephone" autocomplete="off" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model.trim="temp.mobile" autocomplete="off" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="电邮" prop="email">
          <el-input v-model.trim="temp.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input type="textarea" v-model.trim="temp.comment" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="isEdit ? update() : add()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchContactList, createContact, modifyContact, deleteContact } from '@/api/client'
import { contactOptions, contactKV } from '@/utils/options'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
const defaultForm = {
  customerId: '',
  type: '',
  name: '',
  telephone: '',
  mobile: '',
  email: '',
  comment: ''
}

export default {
  name: 'ClientDetail',
  components: { Pagination },
  filters: {},
  data() {
    return {
      tableData: [],
      total: 0,
      listQuery: {
        id: '',
        limit: 10,
        page: 1
      },
      activeName: 'first',
      contactOptions,
      contactKV,
      lockFlagOptions: [
        { key: 0, value: '启用' },
        { key: 1, value: '禁用' }
      ],
      temp: Object.assign({}, defaultForm),
      dialogVisible: false,
      rules: {},
      isEdit: false
    }
  },

  created() {
    // const id = this.$route.params && this.$route.params.id
    // console.log('id==', id)
    // this.listQuery.id = parseInt(id)
    // defaultForm.customerId = parseInt(id)
    // this.fetchList()
  },
  methods: {
    // 得到列表
    async fetchList() {
      this.listLoading = true
      fetchContactList(this.listQuery).then((response) => {
        this.tableData = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.fetchList()
    },

    // 重置数据
    resetTemp() {
      console.log('resetTemp', defaultForm)
      this.temp = Object.assign({}, defaultForm)
    },
    handleAdd() {
      this.resetTemp()
      this.dialogVisible = true
    },

    add() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createContact(this.temp).then((response) => {
            this.$message.success('添加成功')
            this.dialogVisible = false
            this.fetchList()
          })
        }
      })
    },
    // 点击“编辑"按钮
    handleUpdate(row) {
      this.temp = Object.assign(this.temp, row)
      this.isEdit = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改
    update() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          modifyContact(this.temp).then((response) => {
            this.$message.success('修改成功')
            this.dialogVisible = false
            this.fetchList()
          })
        }
      })
    },

    // 删除
    handleDelete(row) {
      this.$confirm(`你确定要删除【${row.name}】吗?`, '提示', {
        type: 'warning'
      })
        .then(() => {
          deleteContact({ id: row.id }).then(() => {
            this.$message.success('删除成功')
            this.fetchList()
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    }
  }
}
</script>
